<?php
namespace Admin\Controller;
use  Think\Controller;

class ApplyController extends Controller{

    public function apply2(){

        //实例化Memebr
        $apply2 = M('apply2');
        // 导入分页类
        import('Org.Util.Page');

        //查询满足满足条件的总记录条数
        $count = $apply2->where('allow = 1')->count();

        //实例化分页类 传入总记录数和每页显示的记录数
        $Page = new \Think\Page($count,10);

        //分页显示输出
        $show = $Page->show();

        $limit = $Page->firstRow . ',' . $Page->listRows;

        $data = M('apply2')->where('allow = 1')->limit($limit)->order('time desc')->select();
        foreach($data as $k=>$v){
            $ids[] = $v['userid'];
            $ids2[] = $v['pid'];
        }

        $getid = array_unique($ids);
        $getid2 = array_unique($ids2);

        $getid = implode(',',$getid);
        $getid2 = implode(',',$getid2);

        if(!$getid){

            $this->display();
            die;
        }

        $res_detail = M('detail1')->where('userid in ('.$getid.')')->select();
        $res_user1 = M('user')->where('id in ('.$getid.')')->select();
        $res_user2 = M('user')->where('id in ('.$getid2.')')->select();

        foreach($data as $k=>$v){

            foreach($res_detail as $k1=>$v1){
                if($v['userid'] == $v1['userid']){

                    $data[$k]['phone'] = $v1['phone'];
                }
            }

            foreach($res_user1 as $k2=>$v2){
                if($v['userid'] == $v2['id']){

                    $data[$k]['user'] = $v2['user'];
                    $data[$k]['sex'] = $v2['sex'];
                }
            }

            foreach($res_user2 as $k3=>$v3){
                if($v['pid'] == $v3['id']){

                    $data[$k]['parent'] = $v3['user'];
                }
            }
        }


        $getlevel = ['1'=>'普通会员','2'=>'vip会员','3'=>'天使合伙人','4'=>'梦想合伙人'];

        foreach($data as $k=>$v){

            $data[$k]['level'] = $getlevel[$v['level']];
            $data[$k]['time'] = date('Y-m-d',$v['time']);
        }

        $this->assign('memberlist',$data);
        $this->assign('page',$show);
        $this->display();
    }

    public function apply3(){

        //实例化Memebr
        $apply3 = M('apply3');
        // 导入分页类
        import('Org.Util.Page');

        //查询满足满足条件的总记录条数
        $count = $apply3->where('allow = 1')->count();

        //实例化分页类 传入总记录数和每页显示的记录数
        $Page = new \Think\Page($count,10);

        //分页显示输出
        $show = $Page->show();

        $limit = $Page->firstRow . ',' . $Page->listRows;

        $data = M('apply3')->where('allow = 1')->limit($limit)->order('time desc')->select();
        foreach($data as $k=>$v){
            $ids[] = $v['userid'];
            $ids2[] = $v['pid'];
        }

        $getid = array_unique($ids);
        $getid2 = array_unique($ids2);

        if(!$getid){
            $this->display();
            die;
        }


        $getid = implode(',',$getid);
        $getid2 = implode(',',$getid2);

        $res_user1 = M('user')->where('id in ('.$getid.')')->select();
        $res_user2 = M('user')->where('id in ('.$getid2.')')->select();

        $detail1_id = [];
        $detail2_id = [];
        $detail3_id = [];
        $detail4_id = [];

        foreach($res_user1 as $k=>$v){

            if($v['level'] == 1){
                $detail1_id[] = $v['detailid'];
            }
            if($v['level'] == 2){
                $detail2_id[] = $v['detailid'];
            }
            if($v['level'] == 3){
                $detail3_id[] = $v['detailid'];
            }
            if($v['level'] == 4){
                $detail4_id[] = $v['detailid'];
            }
        }

        if($detail1_id){
            $detail1_id = array_unique($detail1_id);
            $get1_id = implode(',',$detail1_id);
            $res_detail1 = M('detail1')->where('id in ('.$get1_id.')')->select();
        }

        if($detail2_id){
            $detail2_id = array_unique($detail2_id);
            $get2_id = implode(',',$detail2_id);
            $res_detail2 = M('detail2')->where('id in ('.$get2_id.')')->select();
        }

        if($detail3_id){
            $detail3_id = array_unique($detail3_id);
            $get3_id = implode(',',$detail3_id);
            $res_detail3 = M('detail3')->where('id in ('.$get3_id.')')->select();

        }

        if($detail4_id){
            $detail4_id = array_unique($detail4_id);
            $get4_id = implode(',',$detail4_id);
            $res_detail4 = M('detail4')->where('id in ('.$get4_id.')')->select();
        }


        foreach($data as $k=>$v){

            foreach($res_detail1 as $k1=>$v1){
                if($v['userid'] == $v1['userid']){

                    $data[$k]['phone'] = $v1['phone'];
                }
            }

            foreach($res_detail2 as $k1=>$v1){
                if($v['userid'] == $v1['userid']){

                    $data[$k]['phone'] = $v1['phone'];
                }
            }

            foreach($res_detail3 as $k1=>$v1){
                if($v['userid'] == $v1['userid']){

                    $data[$k]['phone'] = $v1['phone'];
                }
            }

            foreach($res_detail4 as $k1=>$v1){
                if($v['userid'] == $v1['userid']){

                    $data[$k]['phone'] = $v1['phone'];
                }
            }

            foreach($res_user1 as $k2=>$v2){
                if($v['userid'] == $v2['id']){

                    $data[$k]['user'] = $v2['user'];
                    $data[$k]['sex'] = $v2['sex'];
                }
            }

            foreach($res_user2 as $k3=>$v3){
                if($v['pid'] == $v3['id']){

                    $data[$k]['parent'] = $v3['user'];
                }
            }
        }


        $getlevel = ['1'=>'普通会员','2'=>'vip会员','3'=>'天使合伙人','4'=>'梦想合伙人'];

        foreach($data as $k=>$v){

            $data[$k]['level'] = $getlevel[$v['level']];
            $data[$k]['time'] = date('Y-m-d',$v['time']);
        }

        $this->assign('memberlist',$data);
        $this->assign('page',$show);
        $this->display();
    }


    public function apply4(){

        //实例化Memebr
        $apply4 = M('apply4');
        // 导入分页类
        import('Org.Util.Page');

        //查询满足满足条件的总记录条数
        $count = $apply4->where('allow = 1')->count();

        //实例化分页类 传入总记录数和每页显示的记录数
        $Page = new \Think\Page($count,10);

        //分页显示输出
        $show = $Page->show();

        $limit = $Page->firstRow . ',' . $Page->listRows;

        $data = M('apply4')->where('allow = 1')->limit($limit)->order('time desc')->select();
        foreach($data as $k=>$v){
            $ids[] = $v['userid'];
        }

        $getid = array_unique($ids);

        $getid = implode(',',$getid);

        if(!$getid){

            $this->display();
            die;
        }

        $res_user1 = M('user')->where('id in ('.$getid.')')->select();

        $detail1_id = [];
        $detail2_id = [];
        $detail3_id = [];
        $detail4_id = [];

        foreach($res_user1 as $k=>$v){

            if($v['level'] == 1){
                $detail1_id[] = $v['detailid'];
            }
            if($v['level'] == 2){
                $detail2_id[] = $v['detailid'];
            }
            if($v['level'] == 3){
                $detail3_id[] = $v['detailid'];
            }
            if($v['level'] == 4){
                $detail4_id[] = $v['detailid'];
            }
        }

        if($detail1_id){
            $detail1_id = array_unique($detail1_id);
            $get1_id = implode(',',$detail1_id);
            $res_detail1 = M('detail1')->where('id in ('.$get1_id.')')->select();
        }

         if($detail2_id){
            $detail2_id = array_unique($detail2_id);
            $get2_id = implode(',',$detail2_id);
            $res_detail2 = M('detail2')->where('id in ('.$get2_id.')')->select();
         }

         if($detail3_id){
            $detail3_id = array_unique($detail3_id);
            $get3_id = implode(',',$detail3_id);
            $res_detail3 = M('detail3')->where('id in ('.$get3_id.')')->select();
         }

         if($detail4_id){
            $detail4_id = array_unique($detail4_id);
            $get4_id = implode(',',$detail4_id);
            $res_detail4 = M('detail4')->where('id in ('.$get4_id.')')->select();
         }

        foreach($data as $k=>$v){

            foreach($res_detail1 as $k1=>$v1){
                if($v['userid'] == $v1['userid']){

                    $data[$k]['phone'] = $v1['phone'];
                }
            }

            foreach($res_detail2 as $k1=>$v1){
                if($v['userid'] == $v1['userid']){

                    $data[$k]['phone'] = $v1['phone'];
                }
            }

            foreach($res_detail3 as $k1=>$v1){
                if($v['userid'] == $v1['userid']){

                    $data[$k]['phone'] = $v1['phone'];
                }
            }

            foreach($res_detail4 as $k1=>$v1){
                if($v['userid'] == $v1['userid']){

                    $data[$k]['phone'] = $v1['phone'];
                }
            }

            foreach($res_user1 as $k2=>$v2){
                if($v['userid'] == $v2['id']){

                    $data[$k]['user'] = $v2['user'];
                    $data[$k]['sex'] = $v2['sex'];
                }
            }
        }

        $getlevel = ['1'=>'普通会员','2'=>'vip会员','3'=>'天使合伙人','4'=>'梦想合伙人'];

        foreach($data as $k=>$v){

            $data[$k]['level'] = $getlevel[$v['level']];
            $data[$k]['time'] = date('Y-m-d',$v['time']);
        }


        $this->assign('memberlist',$data);
        $this->assign('page',$show);
        $this->display();
    }

    public function reapply(){

        //实例化Memebr
        $apply3 = M('reapply3');
        // 导入分页类
        import('Org.Util.Page');

        //查询满足满足条件的总记录条数
        $count = $apply3->where('allow = 1')->count();

        //实例化分页类 传入总记录数和每页显示的记录数
        $Page = new \Think\Page($count,10);

        //分页显示输出
        $show = $Page->show();

        $limit = $Page->firstRow . ',' . $Page->listRows;

        $data = M('reapply3')->where('allow = 1')->limit($limit)->order('time desc')->select();
        foreach($data as $k=>$v){
            $ids[] = $v['userid'];
            $ids2[] = $v['pid'];
        }

        $getid = array_unique($ids);
        $getid2 = array_unique($ids2);

        $getid = implode(',',$getid);
        $getid2 = implode(',',$getid2);

        if(!$getid){
            $this->display();
            die;
        }


        $res_user1 = M('user')->where('id in ('.$getid.')')->select();
        $res_user2 = M('user')->where('id in ('.$getid2.')')->select();

        $detail1_id = [];
        $detail2_id = [];
        $detail3_id = [];
        $detail4_id = [];

        foreach($res_user1 as $k=>$v){

            if($v['level'] == 1){
                $detail1_id[] = $v['detailid'];
            }
            if($v['level'] == 2){
                $detail2_id[] = $v['detailid'];
            }
            if($v['level'] == 3){
                $detail3_id[] = $v['detailid'];
            }
            if($v['level'] == 4){
                $detail4_id[] = $v['detailid'];
            }
        }

        if($detail1_id){
            $detail1_id = array_unique($detail1_id);
            $get1_id = implode(',',$detail1_id);
            $res_detail1 = M('detail1')->where('id in ('.$get1_id.')')->select();
        }

        if($detail2_id){
            $detail2_id = array_unique($detail2_id);
            $get2_id = implode(',',$detail2_id);
            $res_detail2 = M('detail2')->where('id in ('.$get2_id.')')->select();
        }

        if($detail3_id){
            $detail3_id = array_unique($detail3_id);
            $get3_id = implode(',',$detail3_id);
            $res_detail3 = M('detail3')->where('id in ('.$get3_id.')')->select();
        }

        if($detail4_id){
            $detail4_id = array_unique($detail4_id);
            $get4_id = implode(',',$detail4_id);
            $res_detail4 = M('detail4')->where('id in ('.$get4_id.')')->select();
        }

        foreach($data as $k=>$v){

            foreach($res_detail1 as $k1=>$v1){
                if($v['userid'] == $v1['userid']){

                    $data[$k]['phone'] = $v1['phone'];
                }
            }

            foreach($res_detail2 as $k1=>$v1){
                if($v['userid'] == $v1['userid']){

                    $data[$k]['phone'] = $v1['phone'];
                }
            }

            foreach($res_detail3 as $k1=>$v1){
                if($v['userid'] == $v1['userid']){

                    $data[$k]['phone'] = $v1['phone'];
                }
            }

            foreach($res_detail4 as $k1=>$v1){
                if($v['userid'] == $v1['userid']){

                    $data[$k]['phone'] = $v1['phone'];
                }
            }

            foreach($res_user1 as $k2=>$v2){
                if($v['userid'] == $v2['id']){

                    $data[$k]['user'] = $v2['user'];
                }
            }

            foreach($res_user2 as $k3=>$v3){
                if($v['userid'] == $v3['id']){

                    $data[$k]['parent'] = $v3['user'];
                }
            }
        }


        $getlevel = ['1'=>'普通会员','2'=>'vip会员','3'=>'天使合伙人','4'=>'梦想合伙人'];

        foreach($data as $k=>$v){

            $data[$k]['level'] = $getlevel[$v['level']];
            $data[$k]['time'] = date('Y-m-d',$v['time']);
        }

        $this->assign('memberlist',$data);
        $this->assign('page',$show);
        $this->display();
    }


    public function pass(){

        //实例化Memebr
        $apply2 = M('apply2');
        $apply3 = M('apply3');
        $apply4 = M('apply4');
         // 导入分页类
        import('Org.Util.Page');

        $data1 = M('apply2')->where('allow = 2')->select();
        $data2 = M('apply3')->where('allow = 2')->select();
        $data3 = M('apply4')->where('allow = 2')->select();

        $list = [];
        foreach($data1 as $k=>$v){

            $list[] = $v;
        }

        foreach($data2 as $k=>$v){
            $list[] = $v;
        }

        foreach($data3 as $k=>$v){
            $list[] = $v;
        }

        foreach($list as $k=>$v){
            $ids[] = $v['userid'];
        }

        $getid = array_unique($ids);
        $getid = implode(',',$getid);

        if(!$getid){
            $this->display();
            die;
        }


        $res_user1 = M('user')->where('id in ('.$getid.')')->select();

        foreach($list as $k=>$v){

            foreach($res_user1 as $k2=>$v2){
                if($v['userid'] == $v2['id']){
                    $list[$k]['user'] = $v2['user'];
                }
            }

        }

        $getlevel = ['1'=>'普通会员','2'=>'vip会员','3'=>'天使合伙人','4'=>'梦想合伙人'];

        foreach($list as $k=>$v){
            $list[$k]['level'] = $getlevel[$v['level']];
            $list[$k]['time'] = date('Y-m-d');
        }

        $count = count($list);
        // 调用分页类
        $Page = new \Think\Page($count,10);
        //分页显示输出
        $show = $Page->show();

        $times = [];
        foreach($list as $k=>$v){
            $times[] = $v['time'];
        }

        $times = rsort($times);
        $new_list = [];
        foreach($times as $k=>$v){
            foreach($list as $k1=>$v1){
                if($v = $v1['time']){
                    $new_list[] = $v1;
                }
            }
        }

        $start = $Page->firstRow;
        $end = ($start+$Page->listRows)-1;
        $newlist = [];

        foreach($new_list as $k=>$v){  // 根据分页显示
            if($k >= $start && $k<= $end){
                $newlist[] = $v;
            }
        }


        $this->assign('memberlist',$list);
        $this->assign('page',$show);
        $this->display();
    }

    public function stop(){

        //实例化Memebr
        $apply2 = M('apply2');
        $apply3 = M('apply3');
        $apply4 = M('apply4');
        // 导入分页类
        import('Org.Util.Page');

        $where['allow'] = array('eq',4);
        $where['view'] = array('neq',2);

        $data1 = M('apply2')->where($where)->order('time desc')->select();
        $data2 = M('apply3')->where($where)->order('time desc')->select();
        $data3 = M('apply4')->where($where)->order('time desc')->select();

        $list = [];

        foreach($data1 as $k=>$v){

            $list[] = $v;
        }

        foreach($data2 as $k=>$v){

            $list[] = $v;
        }

        foreach($data3 as $k=>$v){

            $list[] = $v;
        }

        foreach($list as $k=>$v){
            $ids[] = $v['userid'];
        }

        $getid = array_unique($ids);
        $getid = implode(',',$getid);

        if(!$getid){
            $this->display();
            die;
        }

        $res_user1 = M('user')->where('id in ('.$getid.')')->select();

        foreach($list as $k=>$v){
            foreach($res_user1 as $k2=>$v2){
                if($v['userid'] == $v2['id']){

                    $list[$k]['user'] = $v2['user'];
                }
            }
        }

        $getlevel = ['1'=>'普通会员','2'=>'vip会员','3'=>'天使合伙人','4'=>'梦想合伙人'];

        foreach($list as $k=>$v){

            $list[$k]['time'] = date('Y-m-d',$v['time']);
        }

        $count = count($list);

        // 调用分页类
        $Page = new \Think\Page($count,10);
        //分页显示输出
        $show = $Page->show();

        $limit = $Page->firstRow . ',' . $Page->listRows;

        $times = [];
        foreach($list as $k=>$v){
            $times[] = $v['time'];
        }

        $times = rsort($times);
        $new_list = [];
        foreach($times as $k=>$v){

            foreach($list as $k1=>$v1){

                 if($v = $v1['time']){

                     $new_list[] = $v1;
                 }
            }
        }


        // 所有正处于判定状态中的记录
        $get_status = M('status')->where('status = 1')->select();
        $get_status3 = M('status3')->where('task = 1')->select();

        foreach($new_list as $k=>$v){

            foreach($get_status as $k1=>$v1){

                if($v['userid'] == $v1['userid'] && $v['pid'] == $v1['pid']){

                    if($v1['endtime'] <= time()){
                        $new_list[$k]['endtime'] = '已经到期';
                    }
                    $new_list[$k]['endtime'] = date('Y-m-d',$v1['endtime']);
                }
            }

            foreach($get_status3 as $k2=>$v2){
                if($v['userid'] == $v2['userid'] && $v['pid'] == $v2['pid']){

                    if($v2['endtime'] <= time()){
                        $new_list[$k]['endtime'] = '已经到期';
                    }
                    $new_list[$k]['endtime'] = date('Y-m-d',$v2['endtime']);
                }
            }
        }

        foreach($new_list as $k=>$v){

            if(!$v['endtime']){

                $new_list[$k]['endtime'] = '已经到期';
            }
        }

        $start = $Page->firstRow;
        $end = ($start+$Page->listRows)-1;
        $newlist = [];

        foreach($new_list as $k=>$v){

            if($k >= $start && $k<= $end){

                $newlist[] = $v;
            }
        }

        $this->assign('memberlist',$newlist);
        $this->assign('page',$show);
        $this->display();

    }

    public function allow(){

        $level = I('level');
        $apply = I('applyid');
        $int = I('int');

        $table_apply = 'apply'.$level;
        $res_apply = M($table_apply)->where("id = '".$apply."'")->find();

        $pid = $res_apply['pid'];
        $uid = $res_apply['userid'];
        
        if($level == 4){
            // 检测userstatus 状态
            $res_user_find = M('user')->where("id = '".$uid."'")->find();
            $user_table = 'userstatus'.$res_user_find['level'];

            if($res_user_find['level'] !=1 && $res_user_find['level'] != 2){
                $res_status_find = M($user_table)->where('userId = "'.$uid.'"')->find();

                if($res_status_find){

                    if($res_status_find['status'] != 1){

                        $arr_userstatus1['status'] = 1;
                        $res_save_status = M($user_table)->where('userId = "'.$uid.'"')->save($arr_userstatus1);
                    }
                }

                $res_notice3 = M('notice')->where('userId = "'.$uid.'" and allow = 1')->find();

                if($res_notice3){

                    $arr_notice1['allow'] = 2;
                    $res_save_notice = M('notice')->where('userId = "'.$uid.'" and allow = 1')->save($arr_notice1);
                }
            }

            // 分配名额
            $res_user_num = $this->getnum($level,$uid,$pid);

            // 修改等级状态
            $res_return = $this->getupdate($level,$uid);

            // 修改申请表状态
            $arr_apply5['allow'] = 2;// 通过
            $res_success = M('apply4')->where('id = "'.$apply.'"')->save($arr_apply5);

            if($res_return){
//                $this->ajaxReturn(['info'=>'编辑成功','status'=>1]);
                $this->redirect('Apply/apply4','','1','操作成功');

            }else{
//                $this->ajaxReturn(['info'=>'编辑失败','status'=>0]);
                $this->redirect('Apply/apply4','','1','操作失败');
            }
        }

        $res_cutnum = M('cutnum')->where('userId = "'.$pid.'"')->field('sum(num) as number')->select();

        if($level == 3){

            $res_user = M('user')->where('id = "'.$uid.'"')->find();

            $data = getsystem();// 获取系统规定的参数

            if($res_user['level'] == 1){
                $num = $data['firstnum'];
            }else{
                $num = $data['firstnum']-1;
            }

            if($res_cutnum[0]['number'] < $num){

                $res_wrong = $this->wrong($pid,$level,$uid,$apply);

                $this->redirect('Apply/apply3','','3','操作失败,用户名额不足');

//                $data = genBody('0','升级失败');
//                $this->ajaxReturn($data,'JSON');
            }

            $arr_apply2['allow'] = 2;// 允许
            $arr_apply2['view'] = 1;// 允许
            $arr_apply2['reson'] = 1;// 正常

            $res_apply = M('apply3')->where('id = "'.$apply.'"')->save($arr_apply2);

            if($res_user['level'] == $level){
                 // 增加名额 如果userstatus 为2 则修改
                $res_return = $this->checkuserstatus('',$uid,$res_user['level'],$level);
                // 分配名额
                $res_user_num = $this->getnum($level,$uid,$pid,$num);
                // 更改pid
                $res_user_save = $this->getupdate($level,$uid,$pid);

                // 如果上级没有更改申请通告 自动更改
                $arr_notice['userId'] = $pid;
                $arr_notice['objectId'] = $uid;
                $arr_notice['level'] = $level;
                $arr_notice['allow'] = 1;

                $find_notice = M('notice3')->where($arr_notice)->find();

                if($find_notice){

                    $save_notice['allow'] = 2;
                    $res_save_notice =M('notice3')->where('id = "'.$find_notice['id'].'"')->save($save_notice);
                }

                if($res_user_save){
                    $where['noticeId'] = 2;
                    $where['userId'] = $uid;
                    $where['allow'] = 1;

                    $res_notice_list = M('notice')->where($where)->find();

                    if($res_notice_list) {

                        $arr_notice1['allow'] = 2;
                        $res_save_notice = M('notice')->where($where)->save($arr_notice1);
                    }

                    $this->redirect('Apply/apply3','','3','操作成功');
//                    $data = genBody('1','操作成功');
//                    $this->ajaxReturn($data,'JSON');
                }else{
                    $this->redirect('Apply/apply3','','3','操作失败');
//                    $data = genBody('1','操作失败');
//                    $this->ajaxReturn($data,'JSON');
                }
            }else{
                // 分配名额
                $res_user_num = $this->getnum($level,$uid,$pid,$num);
                // 升级成功修改等级
                $res_user_save = $this->getupdate($level,$uid,$pid);
                 // 升级成功  如果上级没有点击允许  自动修改
                $arr_notice['userId'] = $pid;
                $arr_notice['objectId'] = $uid;
                $arr_notice['level'] = $level;
                $arr_notice['allow'] = 1;

                $table = 'notice'.$level;
                $find_notice = M($table)->where($arr_notice)->find();

                if($find_notice){

                    $save_notice['allow'] = 2;
                    $res_save_notice =M($table)->where('id = "'.$find_notice['id'].'"')->save($save_notice);
                }

                if($res_user_save){

                    $this->redirect('Apply/apply3','','3','操作成功');
//
//                    $data = genBody('1','操作成功');
//                    $this->ajaxReturn($data,'JSON');
                }else{

                    $this->redirect('Apply/apply3','','3','操作失败');
//
//                    $data = genBody('1','操作失败');
//                    $this->ajaxReturn($data,'JSON');
                }
            }


        }
    }


    public function remove(){

        $level = I('level');
        $apply = I('id');

        $msg_log = APP_PATH.'remonv.log';
        monitor_log($msg_log,array('level'=>$level,'apply'=>$apply),FILE_APPEND);

        $table_apply = 'apply'.$level;
        $res_apply = M($table_apply)->where("id = '".$apply."'")->find();

        $pid = $res_apply['pid'];
        $uid = $res_apply['userid'];

        $arr_notice['userId'] = $pid;
        $arr_notice['objectId'] = $uid;
        $arr_notice['level'] = $level;
        $arr_notice['allow'] = 1;

        $table = 'notice'.$level;

        if($level != 4){

            $find_notice = M($table)->where($arr_notice)->find();

            if($find_notice){

                $save_notice['allow'] = 3;
                $res_save_notice =M($table)->where('id = "'.$find_notice['id'].'"')->save($save_notice);
            }
        }

        // 后台申请表 状态修改
        $arr_apply1['allow'] = 3;// 不允许操作
        $table = 'apply'.$level;

        $res_apply_save = M($table)->where('id ="'.$apply.'"')->save($arr_apply1);

        if($res_apply_save){

            $this->redirect('Apply/apply3','','3','操作成功');
        }else{

            $this->redirect('Apply/apply3','','3','操作失败');
        }
    }
    
    //成功之后修改等级
    
    public function getupdate($level,$uid,$pid){
        
        if($level == 4){
            
            $arr_user['level'] = 4;
            $arr_user['pid'] = 0;

            $res_user = M('user')->where('id = "'.$uid.'"')->find();
            $table1 = 'detail'.$res_user['level'];
            $table2 = 'pic'.$res_user['level'];

            if($res_user['level'] == 4){

                return 1;
            }

            $find_detail = M($table1)->where('id = "'.$res_user['detailid'].'"')->find();
//            var_dump($find_detail);die;
            $find_pic = M($table2)->where('id = "'.$res_user['picid'].'"')->find();
            
            unset($find_detail['id']);
            unset($find_pic['id']);


            $find_detail['userId'] = $find_detail['userid'];
            $find_detail['useredId'] = $find_detail['useredid'];
            $find_pic['userId'] = $find_pic['userid'];

            $msg_log = APP_PATH.'113.log';
            monitor_log($msg_log,array('user'=>$res_user,'detail'=>$find_detail,'pic'=>$find_pic),FILE_APPEND);
            
            $res_detail = M('detail4')->add($find_detail);
            $res_pic = M('pic4')->add($find_pic);

            $arr_user['detailId'] = $res_detail;
            $arr_user['picId'] = $res_pic;

            $msg_log = APP_PATH.'112.log';
            monitor_log($msg_log,array('detail'=>$find_detail,'pic'=>$find_pic),FILE_APPEND);

            // 修改等级资料

            $save_user = M('user')->where('id = "'.$uid.'"')->save($arr_user);

            if($save_user){
                return 1;
            }else{
                return 0;
            }

        }else{

            $arr_user['level'] = $level;
            $arr_user['pid'] = $pid;

            $res_user = M('user')->where('id = "'.$uid.'"')->find();

            if($level == $res_user['level']){

                return true;
            }

            $table1 = 'detail'.$res_user['level'];
            $table2 = 'pic'.$res_user['level'];


            $find_detail = M($table1)->where('id = "'.$res_user['detailid'].'"')->find();
            $find_pic = M($table2)->where('id = "'.$res_user['picid'].'"')->find();

            unset($find_detail['id']);
            unset($find_pic['id']);

            $table3 = 'detail'.$level;
            $table4 = 'pic'.$level;

            $find_detail['userId'] = $find_detail['userid'];
            $find_detail['useredId'] = $find_detail['useredid'];
            $find_pic['userId'] = $find_pic['userid'];

            $res_detail = M($table3)->add($find_detail);
            $res_pic = M($table4)->add($find_pic);

            $arr_user['detailId'] = $res_detail;
            $arr_user['picId'] = $res_pic;

            $save_user = M('user')->where('id = "'.$uid.'"')->save($arr_user);

            if($save_user){
                return 1;
            }else{
                return 0;
            }

        }
    }

    public function checkuserstatus($pid,$id,$level,$level1){

        // 更新userstatus 状态
        // 更新notice
        $table = 'userstatus'.$level;
        $res_user = M($table)->where('userId = "'.$id.'"')->find();
        $res_notice = M('notice')->where('userId = "'.$id.'"')->find();

        if($level1 == 3){

            if($res_user['status'] == 2){

                $arr_user['status'] = 1;

                $save_user = M($table)->where("userId = '".$id."'")->save($arr_user);
            }

        }else if($level1 == 4){

            if($res_user['status'] == 3){

                $arr_user['status'] = 1;

                $save_user = M($table)->where("userId = '".$id."'")->save($arr_user);
            }
        }

        if($save_user){

            return 1;
        }

    }

    public function wrong($pid,$level,$uid,$applyId){

        $res_notice1 = $this->sendnotice(3,$uid);
        // 修改状态
        $status = 3;
        $res_set_status = $this->setstatus($uid,$status,$level);// 修改上级状态

        $arr_list['userId'] = $uid;
        $arr_list['pid'] = $pid;
        $arr_list['level'] = $level;
        $arr_list['starTime'] = time();
        $arr_list['endTime'] = time()+24*60*60;
        $arr_list['status'] = 1;
        $arr_list['state'] = 1;

        $res_status  = M('status')->add($arr_list);

        // 后台申请表 状态修改
        $arr_apply1['allow'] = 4;// 暂时停止操作
        $arr_apply1['reson'] = 2; // 该上级名额不足
        $table = 'apply'.$level;

        $res_apply_save = M($table)->where('id ="'.$applyId.'"')->save($arr_apply1);
    }


    public function sendnotice($noticeId,$userId){

        $arr_notice['noticeId'] = $noticeId;// 名额不足
        $arr_notice['userId'] = $userId;
        $arr_notice['time'] = time();
        // 当该上级名额或者权限不足时，发送的公告
        $res_notice = M('notice')->add($arr_notice);

        if($res_notice){
            return 1;
        }
    }

    // 当上级名额不足或者权限不够时，设置userstatus表的状态
    // $userId 上级的id;
    // $status  想要修改的状态
    // $level  上级的等级
    public function setstatus($userId,$status,$level){

        $table = 'userstatus'.$level;
        $res_select = M($table)->where("userId = '".$userId."'")->find();

        $arr_userstatus['userId'] = $userId;
        $arr_userstatus['status'] = $status;

        // 修改状态
        if($res_select) {
            $res_save_status = M($table)->where("userId = '".$userId."'")->save($arr_userstatus);
        }else{
            $res_save_status = M($table)->where("userId = '" . $userId . "'")->add($arr_userstatus);
        }
        return $res_save_status;
    }

    // 调整
    public function adjust(){

           $id = I('id');
           $pid = I('pid');
           $level = I('level');
           $res_user = M('user')->where('id = "'.$pid.'"')->find();
           if($res_user['level'] == 4){
//            $data = genBody('1','被申请者等级已经是最高级别，请于平台管理处处理');
//            $this->ajaxReturn($data,'JSON');
               $this->redirect('Apply/stop','','1','该会员调整需要去平台中心');
           }

           $table = 'apply'.$level;
           $where['id'] = $id;
           $where['pid'] = $pid;
           $where['level'] = $level;
           $where['allow'] = 4;
           $res_apply = M($table)->where($where)->find();

           $save['pid'] = $res_user['pid'];
           $save['allow'] = 1;
           $save['view'] = 3;
           $save['reson'] = 1;
           $save['time'] = time();

           // 更新申请表
           $save_apply  = M($table)->where('id = "'.$res_apply['id'].'"')->save($save);

           $table_notice = 'notice'.$level;

           $res_notice = M($table_notice)->where('applyId = "'.$res_apply['id'].'" and userId = "'.$pid.'"')->find();

           if($res_notice && $res_notice['allow'] == 1){

               $arr_notice2['allow'] = 3;// 取消
               $result_notice = M($table_notice)->where('id = "'.$res_notice['id'].'"')->save($arr_notice2);
           }

           $arr_notice3 = $res_notice;
           unset($arr_notice3['id']);

           $arr_notice3['userId'] = $res_user['pid'];
           $arr_notice3['time'] = time();
           $arr_notice['allow'] = 1;

           $table2 = 'notice'.$level;
           // 给新的pid 发送升级公告
           $insert_notice = M($table2)->add($arr_notice3);

           if($insert_notice){

               $data = genBody('1','操作成功');
               $this->ajaxReturn($data,'JSON');
           }

    }


    public function getnum($level,$uid,$pid,$num){

        $data = getsystem();
        if($level == 4){

            $num1['num'] = $data['secondnum'];
            $num1['userId'] = $uid;
            $num1['geterId'] = 0;

            $res_num1 = M('cutnum')->add($num1);

            if($res_num1){

                return 1;
            }
        }else if($level == 3){

            $num2['num'] = $num;
            $num2['userId'] = $uid;
            $num2['geterId'] = $pid;

            $num3['num'] = -$num;
            $num3['userId'] = $pid;
            $num3['geterId'] = $uid;

            M('cutnum')->startTrans();// 开启事务

            $res1 = M('cutnum')->add($num2);
            $res2 = M('cutnum')->add($num3);

            if($res1 && $res2){
                M('cutnum')->commit();
            }else{
                M('cutnum')->rollback();
            }

            $res_user = M('user')->where('id = "'.$uid.'"')->find();

            if($res_user['level'] == 1){

                $num4['num'] = -1;
                $num4['userId'] = $uid;
                $num4['geterId'] = $uid;

                $res3 = M('cutnum')->add($num4);// 因为是直接成为的天使 所以需要消耗一个vip民呃呃
            }

            if($res1 && $res2){

                return 1;
            }
        }
    }

}